#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/6/21 15:41
# ===========================================
#       题目名称： 47. 全排列 II
#       题目地址： https://leetcode.cn/problems/permutations-ii/
#       题目描述： https://note.youdao.com/s/LWYgPSzT
# ===========================================


def item(args):
    pass


class Solution:

    def permuteUnique(self, nums):
        """
            给定一个可包含重复数字的序列 nums ，按任意顺序 返回所有不重复的全排列。
        """
        res = set()
        digit = 1
        while digit <= len(nums):
            if res:
                temp_res = set()
                for tuple_val in res:
                    for cur_index in range(len(nums)):
                        cur_val = nums[cur_index]
                        if cur_val not in tuple_val or tuple_val.count(cur_val) != nums.count(cur_val):
                            list_val = list(tuple_val)
                            list_val.append(cur_val)
                            temp_res.add(tuple(list_val))
                res = temp_res
            else:
                for val in nums:
                    res.add((val,))
            digit += 1
        res = list({val for val in res})
        res = [list(val) for val in res]
        return res


if __name__ == '__main__':
    s = Solution()
    print("nums = [1,1,2] => %s" % s.permuteUnique(nums=[1, 1, 2]))  # [[1,1,2],[1,2,1],[2,1,1]]
    print("nums = [1,2,3] => %s" % s.permuteUnique(nums=[1, 2, 3]))  # [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
